package org.bite.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.bite.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

//    @ResultMap("resultMap")
    @Select("select * from userinfo;")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag(Integer deleteFlag);  //括号里的参数传到上面的SQL语句中,只有一个参数,对应的名称可以不一样

    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoByDeleteFlag2(Integer deleteFlag, Integer gender);

    @Select("select * from userinfo where id = #{id}")
    UserInfo getUserInfoByDeleteFlag3(Integer id);

    @Options(useGeneratedKeys = true, keyProperty = "id")  //拿到字段的id
    @Insert("insert into userInfo (username, password, age, gender) values (#{username}, #{password}, #{age}, #{gender})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into userInfo (username, password, age, gender) values " +
            "(#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userInfo where id = #{id}")
    Integer delete(Integer id);

    @Update("update userInfo set password = #{password}, age = #{age}, gender = #{gender} where id = #{id}")
    Integer update(UserInfo userInfo);

    /*
    * 起别名
    * */
    @Select("SELECT id, username, password, age, gender, phone, delete_flag as deleteFlag, " +
            "create_time as createTime, update_time as updateTime FROM userInfo")
    List<UserInfo> selectUserInfos();

    /*
    * 复用结果映射关系
    * */
    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })
    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos2();
}
